/*
 *  Hashing.h
 *  TubeTruck
 *
 *  Created by Aeturnum on 6/5/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#pragma once

#include <vector>
#include <openssl/sha.h>
#include <stdio.h>

#include "TubeTruckTypes.h"
#include "util.h"

namespace TTP {
   namespace Crypt {
      class Hashing {
         std::vector<byte> mHash;
      protected:
         
         std::vector<byte> hash(std::vector<byte>& packet) const {
            std::vector<byte> hashVal(20);
            SHA1(&packet[0], packet.size(), &hashVal[0]);
//            std::cout << "Hash Value Generated:\n";
//            std::cout << hashVal;
            
            return hashVal;
         }
         
         std::vector<byte> getBlankHash() const {
            std::vector<byte> blank;
            blank.insert(blank.end(), 20, 0);
            return blank;
         }
         
      public:
         Hashing() {}
         ~Hashing(){}
         
         virtual std::vector<byte> getHashablePacket() const = 0;
         
         std::vector<byte> createHash() const {
            std::vector<byte> temp = getHashablePacket();
            return hash(temp);
         }
         
         bool verifyHash() {
            std::vector<byte> newHash(createHash());
//            std::cout << "Old Hash Value:\n";
//            std::cout << mHash;
            return (newHash == mHash);
         }
         
         void hashToVec(std::vector<byte> &vec) const {
            std::vector<byte> hash = createHash();
            vec.insert(vec.end(), hash.begin(), hash.end());   
         }
         
         void hashFromVec(std::vector<byte>::const_iterator &vit) {
            mHash.clear();
            mHash.insert(mHash.end(), vit, vit+20);
            vit = vit + 20;
         }
      };
   }
}